<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>

      <el-form-item prop="username">

        <el-input
          prefix-icon="el-icon-user"
          ref="username"
          onkeyup="value=value.replace(/[^\w\.\/]/ig,'')"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">

        <el-input
          prefix-icon="el-icon-goods"
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />

      </el-form-item>
      <div class="passwordStyle">
        <div> <el-checkbox v-model="checked">记住密码</el-checkbox></div>
<!--        <div style="cursor:pointer" @click="forgetPassword">忘记密码</div>-->
      </div>


      <el-button :disabled="isDisabled" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click="handleLogin">Login</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

import { Message } from 'element-ui';
import {doMd5} from "@/utils/util";
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      isDisabled:false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      checked: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
        // this.$router.push({ path: '/dashboard' })
                  this.$httpService.post('/data/youChu/auth/login',{
                    username:'admin',
                    password:"123456"
                  }).then(res=>{
                  
              // console.log(  this.getMenuList(res))
              window.localStorage.setItem('token',res.token)
              window.localStorage.setItem('uuid',res.uuid)
              this.$router.push({ path: '/' })
          })
        
   this.isDisabled = true
      // this.$httpService.post('/yuanjiu.testnew/yj-admin/backLogin',{
      //   adminAccount:this.loginForm.username,
      //   adminPassword:doMd5(this.loginForm.username + '+'+ this.loginForm.password),
      //   // adminPassword:this.loginForm.password
      // }).then(res => {
      //   // console.log('ress',res)
      //   window.localStorage.setItem('token',res.token)
      //   window.localStorage.setItem('yj_adminId',res.adminId)
      //   window.localStorage.setItem('touxiang',res.adminHeadImage)
      //   // console.log('        res.menuList',  res.menuList)
      //   this.getMenuList(res.menuList)
      //   // console.log('树桩菜单', this.getMenuList(res.menuList))
      //   window.localStorage.setItem('menuList',JSON.stringify(this.getMenuList(res.menuList)))
      //   this.$router.push({ path: '/dashboard' })
      //
      // })


      setTimeout(()=>{
        this.isDisabled = false
      },5000)
      // this.$refs.loginForm.validate(valid => {
      //   if (valid) {
      //     this.loading = true
      //     this.$store.dispatch('user/login', this.loginForm).then(() => {
      //       this.$router.push({ path: '/' })
      //       this.loading = false
      //     }).catch(() => {
      //       this.loading = false
      //     })
      //   } else {
      //     console.log('error submit!!')
      //     return false
      //   }
      // })
    },

      getRoute(uuid){
          // http://badfsadsgrewgf.mynatapp.cc/data/youChu/route/getRouteByUuid?secret=123
          this.$httpService.get('/data/youChu/route/getRouteByUuid?uuid='+uuid).then(res=>{
              console.log(res)
              // console.log(  this.getMenuList(res))
            window.localStorage.setItem('menuList',JSON.stringify(this.getMenuList(res)))
              // window.localStorage.setItem('uuid',res.uuid)
              // this.$router.push({ path: '/dashboard' })
          })

      },
    //忘记密码
    forgetPassword(){
      // console.log('123123');
      this.$router.push({path:'/forgetPassword'})
    },
    //递归树状菜单
    getMenuList(authList) {
      let menu = [];
      let map = {}
      authList.forEach(i => {
        i.children = [];
        map[i.menuId] = i
      })
      authList.forEach(m => {

          console.log('mmmmmm',m)
        m.meta={
          title:"变更咨询师",
          icon: 'el-icon-s-help'
        }
        m.meta.title = m.authName

        if (m.menuParentId == -1) {
          menu.push(map[m.menuId]); // 如果是根 就直接push到menu中
        } else {
          map[m.menuParentId] && map[m.menuParentId].children.push(m);
        }
      });
      return menu
    },
  },
  created() {
    localStorage.clear();
  },
  beforeRouteLeave: (to, from, next) => {
      next()
    // if(!localStorage.getItem('token')){
    //   Message({
    //     showClose: true,
    //     message:'当前状态未登录',   //msg用于传参
    //     type: "error",
    //     center: true,
    //     duration:3000
    //   })
    //   next(false)
    // }
    // else {
    //   next()
    // }
  },
}
</script>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;



/* reset element-ui css */
.login-container {
  //.el-input {
  //  display: inline-block;
  //  height: 47px;
  //  width: 85%;
  //
  //}

  .el-form-item {
border: 1px solid wheat;
    border-radius: 5px;
    color: #454545;
  }
  .el-input__inner{
    border: none;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 60px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    //background-color: white;
    margin-top: 160px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  //密码
  .passwordStyle{
    display: flex;
    justify-content: space-between;
    color: white;
    margin-bottom: 30px;
  }
  .el-checkbox__label {
    display: inline-block;
    padding-left: 10px;
    line-height: 19px;
    font-size: 14px;
    color: white;
  }
}
</style>
